package org.tc.feign.product.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tc.feign.service.FeignProductService;
import org.tc.product.entity.Commoditys;
import org.tc.product.util.PageHelper;


import java.util.ArrayList;
import java.util.List;

//@RequestMapping("/Commodity")
@CrossOrigin(origins = {"*"}, methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.OPTIONS}, allowCredentials = "true")
@RestController
public class CommodityController {

    @Autowired
    private FeignProductService feignProductService;

    @RequestMapping("/selectAllCom")
    public PageHelper<Commoditys> selectAllCom
            (Integer pageNo, Integer pageSize, Integer storeid, Integer state,String brand) {
        return feignProductService.selectAllCom(pageNo, pageSize, storeid, state,brand);
    }

    @RequestMapping("/updcomd")
    public Integer updcom(@RequestBody Commoditys commoditys) {
        return feignProductService.updcom(commoditys);
    }

    @RequestMapping("/insertComd")
    public Integer insertCom(@RequestBody Commoditys commoditys) {
        return feignProductService.insertCom(commoditys);
    }

    @RequestMapping("/selectComById")
    public Commoditys selectComById(Integer id) {
        Commoditys commoditys = feignProductService.selectComById(id);
        return commoditys;
    }

    @RequestMapping("/selectListComById")
    public List<Commoditys> selectListComById(Integer id) {
        List<Commoditys> list = new ArrayList<>();
        list.add(feignProductService.selectComById(id));
        return list;
    }

    @RequestMapping("/getAllCom")
    public List<Commoditys> getAllCom(Integer storeid) {
        return feignProductService.getAllCom(storeid);
    }

    //减库存
    @RequestMapping("/updComStock")
    public Integer updComStock(Integer id, Integer stock) {
        Commoditys commoditys = feignProductService.selectComById(id);
        stock = commoditys.getStock() - stock;
        commoditys = new Commoditys();
        commoditys.setId(id);
        commoditys.setStock(stock);
        int count = feignProductService.updcom(commoditys);
        return count;
    }

    //加库存
    @RequestMapping("/updComStocked")
    public Integer updComStocked(String name, Integer stock, Integer state, Integer storeid) {
        Integer id = feignProductService.getIdByName(name, storeid);
        Commoditys commoditys = feignProductService.selectComById(id);
        stock = commoditys.getStock() + stock;
        commoditys = new Commoditys();
        commoditys.setId(id);
        commoditys.setState(state);
        commoditys.setStock(stock);
        int count = feignProductService.updcom(commoditys);
        return count;
    }

    @RequestMapping("/getIdByNamed")
    public int getIdByName(String name, Integer storeid) {
        return feignProductService.getIdByName(name, storeid);
    }

    //根据分类id查询商品
    @RequestMapping("/getComByClassid")
    public List<Commoditys> getComByClass(Commoditys record) {
        List<Commoditys> list = feignProductService.getComByClass(record);
        return list;
    }

    @RequestMapping("/selectBrand")
    public List<Commoditys> selectBrand( Integer classoneid,  Integer classtweid,  Integer classthreeid){
        return feignProductService.selectBrand(classoneid,classtweid,classthreeid);
    }

    @RequestMapping("/QuerytoSalea")
    @ResponseBody
    public int QuerytoSalea(Integer id,Integer storeid)
    {
        int count=feignProductService.QuerytoSalea(id,storeid);
        return count;
    }

    /**
     * 首页展示销量高的会员商品
     * @param storeid
     * @return
     * 马帅琦
     */
    @RequestMapping("/QueryMem")
    @ResponseBody
    public List<Commoditys> QueryMem(Integer storeid)
    {
        List<Commoditys> list=feignProductService.QueryMem(storeid);
        return list;
    }

    /**
     * 根据分店展示所有会员商品
     * @param storeid
     * @return
     * 马帅琦
     */
    @RequestMapping("/QueryAllMem")
    @ResponseBody
    public List<Commoditys> QueryAllMem(Integer storeid)
    {
        List<Commoditys> list=feignProductService.QueryAllMem(storeid);
        return list;
    }
}
